<template>
    <div>
        <el-form
                v-loading="loading"
                element-loading-text="正在登录。。。"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                ref="loginForm"
                :model="loginForm"
                class="loginContainer"
                :rules="rules">
            <h3 class="loginTitle">系统登录</h3>
            <el-form-item prop="username">
                <el-input type="text" placeholder="请输入用户名" auto-complete="false"
                          v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" placeholder="请输入密码" auto-complete="false"
                          v-model="loginForm.password"></el-input>
            </el-form-item>
            <el-form-item prop="code">
                <el-input type="text" placeholder="点击图片切换验证码" auto-complete="false" v-model="loginForm.code"
                          style="width: 240px;margin-right: 5px"></el-input>
                <img :src="captchaUrl" @click="updateCaptcha">
            </el-form-item>
            <el-checkbox v-model="checked" class="loginRemember">记住我</el-checkbox>
            <el-button type="primary" style="width: 100%" @click="submitForm">登录</el-button>
        </el-form>
    </div>
</template>

<script>


    export default {
        name: "Login",
        data() {
            return {
                //登录表单数据
                loginForm: {
                    username: '',
                    password: '',
                    code: '',
                },
                //是否记住登录信息
                checked: true,
                //图片验证码路径，后边加上时间戳的目的是能够正确刷新验证码
                captchaUrl: 'captcha?time=' + new Date(),
                //校验规则
                rules: {
                    username: [{required: true, message: "请输入用户名", trigger: 'blur'}],
                    password: [{required: true, message: "请输入密码", trigger: 'blur'}],
                    code: [{required: true, message: "请输入验证码", trigger: 'blur'}],
                },
                //判断是否提示正在加载的图案
                loading: false,
            }
        },

        methods: {
            /**
             * 提交表单数据
             */
            submitForm() {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.loading = true;
                        this.postRequest('/login', this.loginForm).then(res => {
                            if (res) {
                                this.loading = false;
                                //获取后端返回的token
                                const tokenStr = res.obj.tokenHead + res.obj.token;
                                //将token存入到sessionStorage中
                                window.sessionStorage.setItem('tokenStr', tokenStr);
                                //页面跳转
                                let path = this.$route.query.redirect;
                                this.$router.replace((path === '/' || path === undefined) ? '/home' : path);
                            }else {
                                //调用更新验证码的方法，更新验证码
                                this.updateCaptcha();
                                //关闭加载动画
                                this.loading = false;
                            }

                        })
                    } else {
                        this.$message.error("请输入完整信息")
                    }
                });
            },
            /**
             * 更好验证码照片
             */
            updateCaptcha() {
                this.captchaUrl = 'captcha?time=' + new Date();
            }
        }
    }
</script>

<style scoped>
    .loginContainer {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 15px 35px 15px;
        background-color: white;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6; /*阴影*/
    }

    .loginTitle {
        margin: 0 auto;
        text-align: center;
    }

    .loginRemember {
        text-align: left;
        margin: 0px 0px 15px 0px;
    }

    .el-form-item__content {
        display: flex;
        align-items: center;
    }
</style>
